package com.taixu.apiorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.taixu.common.core.page.TableDataInfo;
import com.taixu.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.taixu.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.taixu.apiorder.domain.bo.ApiOrderBo;
import com.taixu.apiorder.domain.vo.ApiOrderVo;
import com.taixu.apiorder.domain.ApiOrder;
import com.taixu.apiorder.mapper.ApiOrderMapper;
import com.taixu.apiorder.service.IApiOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单Service业务层处理
 *
 * @author ximu
 * @date 2023-12-14
 */
@RequiredArgsConstructor
@Service
public class ApiOrderServiceImpl implements IApiOrderService {

    private final ApiOrderMapper baseMapper;

    @Override
    public List<ApiOrderVo> queryListByEesc(int page, int size) {
        return baseMapper.queryListByEesc(page,size);
    }

    @Override
    public ApiOrderVo queryByOrder(String orderNo) {
        return baseMapper.queryByOrder(orderNo);
    }

    /**
     * 查询订单
     */
    @Override
    public ApiOrderVo queryById(Long orderId){
        return baseMapper.selectVoById(orderId);
    }

    /**
     * 查询订单列表
     */
    @Override
    public TableDataInfo<ApiOrderVo> queryPageList(ApiOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ApiOrder> lqw = buildQueryWrapper(bo);
        Page<ApiOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单列表
     */
    @Override
    public List<ApiOrderVo> queryList(ApiOrderBo bo) {
        LambdaQueryWrapper<ApiOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ApiOrder> buildQueryWrapper(ApiOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ApiOrder> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getOrderNo()), ApiOrder::getOrderNo, bo.getOrderNo());
        lqw.like(StringUtils.isNotBlank(bo.getCph()), ApiOrder::getCph, bo.getCph());
        lqw.like(StringUtils.isNotBlank(bo.getVin()), ApiOrder::getVin, bo.getVin());
        lqw.eq(bo.getStatus() != null, ApiOrder::getStatus, bo.getStatus());
        lqw.eq(bo.getResStatus() != null, ApiOrder::getResStatus, bo.getResStatus());
        lqw.eq(bo.getPushStatus() != null, ApiOrder::getPushStatus, bo.getPushStatus());
        lqw.like(StringUtils.isNotBlank(bo.getBykey()), ApiOrder::getBykey, bo.getBykey());
        lqw.eq(bo.getFoundTime() != null, ApiOrder::getFoundTime, bo.getFoundTime());
        lqw.eq(bo.getFinishTime() != null, ApiOrder::getFinishTime, bo.getFinishTime());
        return lqw;
    }

    /**
     * 新增订单
     */
    @Override
    public Boolean insertByBo(ApiOrderBo bo) {
        ApiOrder add = BeanUtil.toBean(bo, ApiOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOrderId(add.getOrderId());
        }
        return flag;
    }

    /**
     * 修改订单
     */
    @Override
    public Boolean updateByBo(ApiOrderBo bo) {
        ApiOrder update = BeanUtil.toBean(bo, ApiOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ApiOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
